package com.flower.controller;

/*
 *@ClassName GoodsController
 *@Description 实现负载均衡
 *@Author 吃麻椒的花酱
 *@Date 2021/5/20 23:19
 *@Version 1.0
 */

import Rule.RandomRuleRuleConfig;
import Rule.RoundRobinRuleConfig;
import com.flower.service.OrderFeign;
import com.flower.util.ResponseResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RibbonClients ({ //设置不同的轮询算法
        @RibbonClient(name = "client-order",configuration= RoundRobinRuleConfig.class),
        @RibbonClient(name = "client-user",configuration= RandomRuleRuleConfig.class)

}
)
public class GoodsController {
    @Autowired
    RestTemplate restTemplate;

    @Resource
    OrderFeign orderFeign;

    @Autowired
    DiscoveryClient discoveryClient;

    @RequestMapping("getGoods")
    public ResponseResult getGoods()
    {
        List<ServiceInstance> instances = discoveryClient.getInstances("client-user");
        String serviceId = instances.get(0).getServiceId();
        return  ResponseResult.success("操作成功",restTemplate.getForObject("http://"+serviceId+"/getUser",Object.class));
    }

}
